package com.kmist.xiakexing.presenters;

import android.util.Log;

import com.kmist.xiakexing.data.EnemyDao;
import com.kmist.xiakexing.interfaces.IEnemyCallback;
import com.kmist.xiakexing.interfaces.IEnemyPresenter;
import com.kmist.xiakexing.interfaces.IMapCallback;
import com.kmist.xiakexing.main.socket.DataRecver;
import com.kmist.xiakexing.main.socket.DataSender;
import com.kmist.xiakexing.main.socket.ISocketCallback;
import com.kmist.xiakexing.main.socket.SockPresenter;
import com.kmist.xiakexing.main.socket.cmd;
import com.kmist.xiakexing.main.socket.retCode;
import com.kmist.xiakexing.tools.Config;
import com.kmist.xiakexing.utils.MapExitBean;
import com.kmist.xiakexing.utils.MidEnemyBean;
import com.kmist.xiakexing.utils.scene.EnemyBean;
import com.kmist.xiakexing.utils.scene.GameScene;
import com.kmist.xiakexing.utils.scene.MapBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class EnemyPresenter implements IEnemyPresenter, ISocketCallback {

    private static final String TAG = "EnemyPresenter";
    private static EnemyPresenter sInstance = null;
    private final EnemyDao enemyDao = null;
    private final SockPresenter sockPresenter;
    IEnemyCallback callback;

    public EnemyPresenter() {
        sockPresenter = SockPresenter.getInstance();
        sockPresenter.setCallback(this);
    }

    public static EnemyPresenter getsInstance() {
        if(sInstance == null){
            synchronized (EnemyPresenter.class){
                sInstance = new EnemyPresenter();
            }
        }
        return sInstance;
    }

    @Override
    public void addEnemy(EnemyBean enemyBean) {

    }

    @Override
    public void addEnemyList(List<EnemyBean> enemyBeans) {

    }

    @Override
    public void delEnemy(EnemyBean enemyBean) {

    }

    @Override
    public void getEnemy(String pname) {

    }

    /**
     * 获取当前地图中的所有敌人
     * @param mapid 地图id
     */
    @Override
    public void getMapEnemy(int mapid) {
        try {
            JSONObject jsonObject = Config.val2json("nowmid", mapid);
            if(mapid != 0){
                DataSender info = new DataSender(cmd.CMD_ENEMY_GETMAPENEMY, jsonObject.toString());
                sockPresenter.sendMessage(info.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateMidEnemy(int mid) {

    }

    @Override
    public void registerViewCallback(IEnemyCallback callback) {
        this.callback = callback;
    }

    @Override
    public void unRegisterViewCallback(IEnemyCallback callback) {

    }

    @Override
    public void onConnected() {
        //NOTODO
    }

    @Override
    public void onNetworkError() {
        //TODO
    }

    @Override
    public void onDataRecved(DataRecver dataRecver) {
        if (!dataRecver.getResp_cmd().substring(0,2).equals(cmd.CMD_ENEMY)) {
            return;
        }

        switch (dataRecver.getResp_code()){

            case retCode.RET_OK:{
                if(dataRecver.getResp_cmd().equals(cmd.CMD_ENEMY_GETMAPENEMY)){
                    getMapEnemy_handler(dataRecver);
                }else{
                    Log.d("RECV:"," 操作成功！");
                }
            } break;
            case retCode.RET_NONE: /* do nothing ,but success */
                callback.onEnemyLoadEmpty();
                break;
            default:
                Log.d("RECV:","cmd exe ERROR: "+dataRecver.getResp_code());
                break;

        }
    }

    private void getMapEnemy_handler(DataRecver dataRecver) {
        List<MidEnemyBean> MEBs = new ArrayList<>();
        if(parseOnlineMapEnemyMsg(dataRecver.getResp_data(), MEBs)){
            //sCache.put(onlineMap.getId(), onlineMap);
           callback.onMapEnemiesLoaded(MEBs);
        }else{
            callback.onEnemyLoadError();
        }
    }

    private boolean parseOnlineMapEnemyMsg(String resp_data, List<MidEnemyBean> lists) {

        try {
            JSONObject jroot = new JSONObject(resp_data);
            JSONArray list = jroot.getJSONArray("midEnemy");

            for(int i =0; i < list.length(); i++){
                JSONObject value = list.getJSONObject(i);

                MidEnemyBean tmp = new MidEnemyBean();
                tmp.setId(value.getString("id"));
                tmp.setName(value.getString("name"));
                tmp.setSid(value.getString("sid"));
                tmp.setMid(value.getInt("mid"));
                tmp.setGyid(value.getString("gyid"));
                tmp.setExp(value.getInt("exp"));
                tmp.setLv(value.getInt("lv"));
                tmp.setHp(value.getInt("hp"));
                tmp.setMaxhp(value.getInt("maxhp"));
                tmp.setAttack(value.getInt("attack"));
                tmp.setDefence(value.getInt("defence"));
                tmp.setBaoji(value.getInt("baoji"));
                tmp.setXixue(value.getInt("xixue"));
                lists.add(tmp);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}
